﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.DataObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class PersonDO : EntityBase
    {
        private string _FirstName;

        /// <summary>
        /// 
        /// </summary>
        public PersonDO()
            : base(EntityType.Persons, "PersonID", "FullName")
        {
        }

        #region props
        private int _PersonID;
        /// <summary></summary>
        public int PersonID
        {
            get
            {
                return this._PersonID;
            }
            set
            {
                this._PersonID = value;
            }
        }

        private string _FullName;
        /// <summary></summary>
        public string FullName
        {
            get
            {
                if (string.IsNullOrEmpty(this._FullName))
                    return this.LastName + ", " + this.FirstName;
                else
                    return this._FullName;
            }
            set
            {
                this._FullName = value;
            }
        }

        /// <summary></summary>
        public string FirstName
        {
            get
            {
                return this._FirstName;
            }
            set
            {
                this._FirstName = value;
            }
        }

        private string _LastName;
        /// <summary></summary>
        public string LastName
        {
            get
            {
                return this._LastName;
            }
            set
            {
                this._LastName = value;
            }
        }

        private string _MidName;
        /// <summary></summary>
        public string MidName
        {
            get
            {
                return this._MidName;
            }
            set
            {
                this._MidName = value;
            }
        }

        private string _SSN;
        /// <summary></summary>
        public string SSN
        {
            get
            {
                return this._SSN;
            }
            set
            {
                this._SSN = value;
            }
        }

        private DateTime _DOB;
        /// <summary></summary>
        public DateTime DOB
        {
            get
            {
                return this._DOB;
            }
            set
            {
                this._DOB = value;
            }
        }

        private string _Title;
        /// <summary></summary>
        public string Title
        {
            get
            {
                return this._Title;
            }
            set
            {
                this._Title = value;
            }
        }

        private string _NameSuffix;
        /// <summary></summary>
        public string NameSuffix
        {
            get
            {
                return this._NameSuffix;
            }
            set
            {
                this._NameSuffix = value;
            }
        }

        private string _Degree;
        /// <summary></summary>
        public string Degree
        {
            get
            {
                return this._Degree;
            }
            set
            {
                this._Degree = value;
            }
        }

        private string _Email;
        /// <summary></summary>
        public string Email
        {
            get
            {
                return this._Email;
            }
            set
            {
                this._Email = value;
            }
        }

        private string _Telephone;
        /// <summary></summary>
        public string Telephone
        {
            get
            {
                return this._Telephone;
            }
            set
            {
                this._Telephone = value;
            }
        }
        #endregion 

        #region Overrides of EntityBase

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="entityNode"></param>
        public override void Save(ref XmlDocument xDoc, XmlNode entityNode)
        {
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode,"Score",this.Score.ToString());
            XmlDataUtil.UpdateAttribute(
                ref xDoc, entityNode, "Flag", string.IsNullOrEmpty(this.Flag) ? "" : this.Flag.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "PersonID",this.PersonID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode,"FirstName", this.FirstName);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "LastName", this.LastName);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "MidName", this.MidName);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Title", this.Title);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "NameSuffix", this.NameSuffix);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Degree", this.Degree);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "DOB", this.DOB.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "SSN", this.SSN);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Email", this.Email);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Telephone", this.Telephone);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "CollapsedID",this.CollapsedID.ToString());
        }

        

        #endregion

        public static List<EntityBase> ReadPersons(XmlDocument xDoc)
        {
            List<EntityBase> persons = new List<EntityBase>();
            XmlNodeList personNodes = xDoc.DocumentElement.SelectNodes("//Person");
            if(personNodes !=null && personNodes.Count>0)
            {
                foreach(XmlNode personNode in personNodes)
                {
                    PersonDO person = PersonDO.Read(personNode);
                    persons.Add(person);
                }
            }
            return persons;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="personNode"></param>
        /// <returns></returns>
        public static PersonDO Read(XmlNode personNode)
        {
            PersonDO person=new PersonDO();
            person.Score = double.Parse(XmlDataUtil.GetAttributeValue(personNode, "Score", "0"));
            person.Flag = XmlDataUtil.GetAttributeValue(personNode, "Flag", "");
            person.PersonID = int.Parse(XmlDataUtil.GetAttributeValue(personNode, "PersonID", "0"));
            person.FirstName = XmlDataUtil.GetAttributeValue(personNode, "FirstName", "");
            person.LastName = XmlDataUtil.GetAttributeValue(personNode, "LastName", "");
            person.MidName = XmlDataUtil.GetAttributeValue(personNode, "MidName", "");
            person.Title = XmlDataUtil.GetAttributeValue(personNode, "Title", "");
            person.NameSuffix = XmlDataUtil.GetAttributeValue(personNode, "NameSuffix", "");
            person.Degree = XmlDataUtil.GetAttributeValue(personNode, "Degree", "");
            person.DOB = DateTime.Parse(XmlDataUtil.GetAttributeValue(personNode, "DOB", DateTime.Now.ToString()));
            person.SSN = XmlDataUtil.GetAttributeValue(personNode, "SSN", "");
            person.Email = XmlDataUtil.GetAttributeValue(personNode, "Email", "");
            person.Telephone = XmlDataUtil.GetAttributeValue(personNode, "Telephone", "");
            person.CollapsedID = int.Parse(XmlDataUtil.GetAttributeValue(personNode, "CollapsedID", "0"));

            return person;
        }
    }
}
